package pets

import (
	"errors"
	"sync"

	"github.com/xinglou123/pkg/db/xorm"
	"github.com/xinglou123/pkg/os/conv"
	"github.com/xinglou123/pkg/utils"
)

type Phrase struct {
	Id         int64  `xorm:"pk autoincr 'id'" form:"id" json:"id"`
	Name       string `xorm:"char(100)" form:"name" json:"name"`
	Deleted_at string `xorm:"DateTime deleted" form:"deleted_at" json:"deleted_at" time_format:"2006-01-02 15:04:05"`
	Created_at string `xorm:"DateTime created" form:"created_at" json:"created_at" time_format:"2006-01-02 15:04:05"`
	Updated_at string `xorm:"DateTime updated" form:"updated_at" json:"updated_at" time_format:"2006-01-02 15:04:05"`
}

// phraseService
var PhraseService = &phraseService{
	mutex: &sync.Mutex{},
}

//phraseService ...
type phraseService struct {
	mutex *sync.Mutex
}

//根据Id 获取
func (service *phraseService) One(uid int64) (*Phrase, error) {
	service.mutex.Lock()
	defer service.mutex.Unlock()
	if uid == 0 {
		return nil, errors.New("缺少参数")
	}
	var phrase Phrase
	orm := xorm.MustDB()
	_, err := orm.Id(uid).Get(&phrase)
	return &phrase, err
}
func (service *phraseService) Query(param map[string]interface{}) ([]Phrase, *utils.Page, error) {
	service.mutex.Lock()
	defer service.mutex.Unlock()

	orm := xorm.MustDB()
	t := orm.Where("id>0")

	if path, ok := param["name"]; ok {
		//存在
		if len(path.(string)) > 0 {
			t = t.Where("name like ?", "%"+path.(string)+"%")
		}
	}
	var phrase []Phrase
	var page int = 1
	if pagek, ok := param["page"]; ok {
		page = conv.Int(pagek)
	}
	var limit int = 10
	if limitk, ok := param["limit"]; ok {
		limit = conv.Int(limitk)
		if limit == 0 {
			limit = 10
		}
	}
	offset := (page - 1) * limit
	count, err := t.Limit(limit, offset).FindAndCount(&phrase)
	if err != nil {
		return phrase, nil, err
	}
	return phrase, utils.NewPage(page, limit, conv.Int(count)), err
}

//
func (service *phraseService) Add(phrase Phrase) (p Phrase, err error) {
	service.mutex.Lock()
	defer service.mutex.Unlock()

	orm := xorm.MustDB()
	session := orm.NewSession()
	defer session.Close()
	serr := session.Begin()

	_, serr = session.InsertOne(phrase)
	if serr != nil {
		err = errors.New("添加失败")
		session.Rollback()
		return p, err
	}
	serr = session.Commit()
	if serr != nil {
		err = errors.New("添加失败")
		return p, err
	}
	_, err = orm.Where("name = ?", phrase.Name).Get(&p)

	return p, err

}

/**
* 删除
*
* @param  int
 */
func (service *phraseService) Delete(uid int64) (int64, error) {
	service.mutex.Lock()
	defer service.mutex.Unlock()
	if uid == 0 {
		return 0, errors.New("不能删除该记录")
	}
	var phrase Phrase
	orm := xorm.MustDB()
	iid, err := orm.Id(uid).Delete(phrase)
	return iid, err
}

/**
* 更新
*
* @param  int
* @return int
 */
func (service *phraseService) Update(phrase Phrase) (iid int64, err error) {
	service.mutex.Lock()
	defer service.mutex.Unlock()

	if phrase.Id == 0 {
		return 0, errors.New("不能修改该记录")
	}
	orm := xorm.MustDB()
	session := orm.NewSession()
	defer session.Close()
	err = session.Begin()

	iid, err = session.Where("id = ?", phrase.Id).Update(phrase)
	if err != nil {
		err = errors.New("更新失败")
		session.Rollback()
		return iid, err
	}
	err = session.Commit()
	if err != nil {
		err = errors.New("更新失败")
		return 0, err
	}
	return iid, err

}

//Count
func (service *phraseService) Count() (int64, error) {
	service.mutex.Lock()
	defer service.mutex.Unlock()

	var phrase Phrase
	orm := xorm.MustDB()
	total, err := orm.Count(phrase)
	return total, err
}
